package com.mall.service.marketing.controller;

import com.mall.commons.common.CommonResult;
import com.mall.model.domain.SmsHomeAdvertise;
import com.mall.model.vo.PageResult;
import com.mall.service.marketing.service.SmsHomeAdvertiseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/home/advertise")
public class SmsHomeAdvertiseController {

    @Autowired
    private SmsHomeAdvertiseService smsHomeAdvertiseService;

    /**
     * 分页查询广告
     * @param name
     * @param type
     * @param endTime
     * @param pageSize
     * @param pageNum
     * @return
     */
    @GetMapping("/list")
    public CommonResult list(String name, Integer type, String endTime,
                             @RequestParam(defaultValue = "5") Integer pageSize,
                             @RequestParam(defaultValue = "1") Integer pageNum){
        PageResult pr = smsHomeAdvertiseService.findList(name,type,endTime,pageNum,pageSize);
        return CommonResult.success(pr);
    }

    /**
     * 添加广告
     * @param advertise
     * @return
     */
    @PostMapping("/create")
    public CommonResult create(@RequestBody SmsHomeAdvertise advertise){
        smsHomeAdvertiseService.save(advertise);
        return CommonResult.success("添加成功");
    }

    /**
     * 删除广告
     * @param ids
     * @return
     */
    @PostMapping("/delete")
    public CommonResult deleteAdvice(@RequestParam List<Integer> ids){

        smsHomeAdvertiseService.removeByIds(ids);
        return CommonResult.success("删除成功");
    }

    /**
     * 修改上下线状态
     * @param id
     * @param status
     * @return
     */
    @PostMapping("/update/status/{id}")
    public CommonResult updateStatus(@PathVariable Integer id,Integer status){

        SmsHomeAdvertise advertise = smsHomeAdvertiseService.getById(id);
        advertise.setStatus(status);
        smsHomeAdvertiseService.updateById(advertise);
        return CommonResult.success("修改状态成功");
    }

    /**
     * 修改广告
     * @param id
     * @param advertise
     * @return
     */
    @PostMapping("/update/{id}")
    public CommonResult updateAdvertise(@PathVariable Integer id,@RequestBody SmsHomeAdvertise advertise){
        smsHomeAdvertiseService.updateAdvertise(id,advertise);
        return CommonResult.success("修改成功");
    }

    /**
     * 获取广告详情
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public CommonResult findAdvertise(@PathVariable Integer id){
        SmsHomeAdvertise advertise = smsHomeAdvertiseService.getById(id);
        return CommonResult.success(advertise);
    }
}
